/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xcyg.fileHandel;

import com.xcyg.util.SysConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.codec.binary.StringUtils;

/**
 *
 * @author cuixubin
 */
public class DownFile extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String filePath = request.getParameter("path");
        if(null == filePath || filePath.equals("")) {
            response.getWriter().print("file not found");
            return;
        }
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String sourcePath = SysConfig.getValue("SOURCE_PATH", "C:/upload") + filePath;
        File f = new File(sourcePath);
        if (f.exists()) {
            FileInputStream filein = new FileInputStream(f);
            byte[] b = new byte[1024];
            try {
                int len = filein.read(b);
                if (fileName != null) {
                    fileName = new String(fileName.getBytes(), "ISO8859-1");
                    if (fileName != "") {
                        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
                    }
                }
                while (len > 0) {
                    response.getOutputStream().write(b, 0, len);
                    len = filein.read(b);
                }
            } catch (Exception e) {
                System.out.println(e.getStackTrace() + e.getMessage());
            } finally {
                response.getOutputStream().flush();
                response.getOutputStream().close();
                filein.close();
            }
        }else {
            response.getWriter().print("Download failed.The file is corrupt or has been deleted.");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
